#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
@Author   : long.hai
@Contact  : 1982964634@qq.com
"""
from typing import Any
from playwright.async_api import Playwright, Page, Browser

from _base.page import PageAction
from _base.session import SessionState


class BrowserAction(PageAction):

    def __init__(self, browser, ws):
        self.browser: Browser = browser
        self.context = None
        self.page = None
        self.ws = ws
        PageAction.__init__(self, self.context, self.page, self.ws)

    async def get_context_by_index(self, index):
        return self.browser.contexts[index]

    async def get_debug_page(self):
        await self.log_info("获取第一个会话的第一个页面。")
        self.context = await self.get_context_by_index(0)
        self.page = self.context.pages[0]
        self.page.set_default_timeout(30000)
        await self.log_info("获取成功页面成功。")

    async def close_browser(self, *args, **kwargs):
        try:
            await self.log_info("即将关闭浏览器。")
            await self.browser.close()
            await self.log_info("关闭浏览器成功。")
        except:
            raise ValueError("关闭浏览器失败。")
